<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-demo__hzproduct}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.linkUrl" placeholder="链接" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="dataForm.status" :placeholder="$t('product.status')" clearable>
            <el-option :label="$t('product.status0')" :value="0"></el-option>
            <el-option :label="$t('product.status1')" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <!--<el-form-item>-->
          <!--<el-button type="info" @click="exportHandle()">{{ $t('export') }}</el-button>-->
        <!--</el-form-item>-->
        <el-form-item>
          <el-button v-if="$hasPermission('hz:product:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('hz:product:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="id" label="序号" header-align="center" align="center"></el-table-column>
        <el-table-column prop="linkUrl" label="链接" header-align="center" align="center"></el-table-column>
        <el-table-column prop="hourIncome" label="每日收益率" header-align="center" align="center"></el-table-column>
        <el-table-column prop="urlAmt" label="link价格" header-align="center" align="center"></el-table-column>
        <el-table-column prop="img" label="广告图片" header-align="center" align="center">
          <template slot-scope="scope" v-if="scope.row.img">
            <template v-if="scope.row.img.indexOf('，') != -1">
              <img style="width: 100%;height: auto;display: block;margin-bottom: 10px;" v-for="(item, index) in scope.row.img.split('，')" :key="index" :src="item" alt="">
            </template>
            <template v-else>
              <img style="width: 100%;height: auto;display: block;margin-bottom: 10px;" v-for="(item, index) in scope.row.img.split(',')" :key="index" :src="item" alt="">
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 1" size="small" type = "success">{{ $t('product.status1') }}</el-tag>
            <el-tag v-else size="small" type="info">{{ $t('product.status0') }}</el-tag>
          </template>
        </el-table-column>

        <!--<el-table-column prop="createTime" label="创建时间" header-align="center" align="center"></el-table-column>-->
        <!--<el-table-column prop="createBy" label="创建人" header-align="center" align="center"></el-table-column>-->
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('hz:product:update')" type="success" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="scope.row.status === 1" type="danger" size="small" @click="stop(scope.row.id)">{{ $t('product.updateStatus0') }}</el-button>
            <el-button v-if="scope.row.status === 0" type="success" size="small" @click="run(scope.row.id)">{{ $t('product.updateStatus1') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './hzproduct-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/hz/product/page',
        getDataListIsPage: true,
        exportURL: '/hz/product/export',
        deleteURL: '/hz/product',
        deleteIsBatch: true,
        run:'/hz/product/run/',
        stop:'/hz/product/stop/'
      },
      dataForm: {
          linkUrl: '',
          status:null
      }
    }
  },
  components: {
    AddOrUpdate
  },
  methods:{
      stop(id) {

          this.$confirm(this.$t('prompt.info', {'handle': this.$t('product.updateStatus0')}), this.$t('prompt.title'), {
              confirmButtonText: this.$t('confirm'),
              cancelButtonText: this.$t('cancel'),
              type: 'warning'
          }).then(() => {
              this.$http['get'](`${this.mixinViewModuleOptions.stop}` + id).then(({ data: res }) => {
                  if (res.code !== 0) {
                      return this.$message.error(res.msg)
                  }
                  this.$message({
                      message: this.$t('prompt.success'),
                      type: 'success',
                      duration: 500,
                      onClose: () => {
                          this.query()
                      }
                  })
              }).catch(() => {
              })
          }).catch(() => {
          })
      },
      run(id) {

          this.$confirm(this.$t('prompt.info', {'handle': this.$t('product.updateStatus1')}), this.$t('prompt.title'), {
              confirmButtonText: this.$t('confirm'),
              cancelButtonText: this.$t('cancel'),
              type: 'warning'
          }).then(() => {
              this.$http['get'](`${this.mixinViewModuleOptions.run}` + id).then(({ data: res }) => {
                  if (res.code !== 0) {
                      return this.$message.error(res.msg)
                  }
                  this.$message({
                      message: this.$t('prompt.success'),
                      type: 'success',
                      duration: 500,
                      onClose: () => {
                          this.query()
                      }
                  })
              }).catch(() => {
              })
          }).catch(() => {
          })
      }
  }
}
</script>
